package com.example.musicplayer.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.musicplayer.R
import com.example.musicplayer.adapter.BannerAdapter.BannerViewHolder
import com.example.musicplayer.model.Banner

/**
 * 轮播图适配器
 */
class BannerAdapter(private val bannerList: List<Banner>) :
    RecyclerView.Adapter<BannerViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BannerViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.banner_item, parent, false)
        return BannerViewHolder(view)
    }

    override fun onBindViewHolder(holder: BannerViewHolder, position: Int) {
        val banner = bannerList[position]
        Glide.with(holder.itemView.context)
            .load(banner.bannerImgUrl)
            .error(R.drawable.ic_launcher_background) // 错误图
            .into(holder.bannerImageView)
    }

    override fun getItemCount(): Int {
        return bannerList.size
    }

    class BannerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val bannerImageView: ImageView

        init {
            bannerImageView = itemView.findViewById(R.id.banner_image)
        }
    }
}